#coding:utf-8
import pickle

#先搞个密码登录才能进入管理
def log_in():
    manager_dict = read_passwd()
    count = 0
    while count < 3:
        user_name = input("请输入你的用户名：")
        user_passwd = input("请输入你的密码：")
        if user_name in manager_dict.keys() and manager_dict[user_name] == user_passwd:
            print("成功登录!")
            return 0
        else:
            count += 1
            print("用户名或者密码错误，请重试（你只有三次机会！！）")
            continue
    else:
        print("登录错误，请重新登录!")
        return 1




def read_passwd():  # 管理员的密码读取函数
    try:
        with open("manager_passwd.pkl", "rb") as f:
                return pickle.load(f)
    except IOError as e:
        return e
    except Exception as e:
        raise e

def save_passwd(manager_pass_dict):
    try:
        with open("manager_passwd.pkl", "wb") as f:
            pickle.dump(manager_pass_dict, f)
    except IOError as e:
        return 1
    except Exception as e:
        raise e


def modify_login_passwd():

    print("----欢迎登录熊尧同学的成绩管理系统----")
    code = log_in()

    while True:
        manager_dict = read_passwd()
        if code == 0:  #说明首先是登录成功了
            print("""
            1、修改密码
            2、退出
            
            """)
            num_input = input("请输入你的操作数:")
            if int(num_input) == 1:
                user_name = input("输入你的用户名：")
                user_passwd = input("请输入你要修改的密码：")
                user_passwd_repeat = input("请再次确认你的密码：")
                if user_passwd != user_passwd_repeat:
                    print("两次密码不一致,操作失败！")
                    continue
                elif user_name in manager_dict.keys():
                    manager_dict[user_name] = user_passwd_repeat
                    save_passwd(manager_dict)
                    return 0
                else:
                    print("用户名不存在！")
                    break

            else:
                print("成功退出！！")
                break



